/**
 * 
 */
package pe.gob.foncodes.sega.bs.util;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
 * CLASE CON FUNCIONES ESTATICAS DE MAYOR USO
 * 
 * @author  Carlos Aguilar
 * @version 2.0, 21/03/2012 11:09:32 AM
 *
 * 
 *  /----------Nombre----------/----fecha----/-------------Motivo--------------/
 *  Carlos Aguilar Chamochumbi	21/03/2012 11:09:32 AM	  Creaci�n de la clase
 *
 */
public class FuncionesSaticas implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3675243726536427783L;

	/**
	 * 
	 */
	public FuncionesSaticas() {
		// TODO Auto-generated constructor stub
	}


	public static void copyPropertiesObject(Object a, Object b) {
		try {
			if (a == null || b == null)
				return;
			Field camposdea[] = b.getClass().getDeclaredFields();
			for (int i = 0; i < camposdea.length; i++) {
				String camponame = camposdea[i].getName();
				String sgetMetod = "get"
						+ Character.toUpperCase(camponame.charAt(0))
						+ camponame.substring(1);
				String ssetMetod = "set"
						+ Character.toUpperCase(camponame.charAt(0))
						+ camponame.substring(1);
				Class<?>[] types = new Class[] {};
				try {
					Method method = b.getClass().getMethod(sgetMetod, types);
					Object valuecampoa = method.invoke(b, new Object[0]);
					Class<?> types2 = camposdea[i].getType();
					Method method2 = a.getClass().getMethod(ssetMetod, types2);
					method2.invoke(a, valuecampoa);
				} catch (NoSuchMethodException exception) {
					// System.out.println("Error NoSuchMethodException: " +
					// exception.getMessage());
				}
			}
		} catch (Exception e) {
			System.out.println("Error exception: " + e.getMessage());
			e.printStackTrace();
		}
	}

	public static String getMimeName(String name) {
		String mime = "";
		int extDot = name.lastIndexOf('.');
		if (extDot > 0) {
			String extension = name.substring(extDot + 1).toLowerCase();
			if ("bmp".equals(extension)) {
				mime = "image/bmp";
			} else if ("jpg".equals(extension)) {
				mime = "image/jpeg";
			} else if ("gif".equals(extension)) {
				mime = "image/gif";
			} else if ("png".equals(extension)) {
				mime = "image/png";
			} else if ("docx".equals(extension)) {
				mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
			} else if ("xlsx".equals(extension)) {
				mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
			} else if ("xls".equals(extension)) {
				mime = "application/vnd.ms-excel";
			} else if ("xml".equals(extension)) {
				mime = "application/xml";
			} else if ("pdf".equals(extension)) {
				mime = "application/pdf";
			} else if ("doc".equals(extension)) {
				mime = "application/msword";
			} else {
				mime = "image/unknown";
			}
		}
		return mime;
	}

	public static String getFileNameSistema(String numeroSid,
			String numeroAnio, Long idusuario, Long idarea, Long idflujo,
			int i, String solofilenameoriginal) {
		int eslashDot = solofilenameoriginal.lastIndexOf("\\");
		
		String filenameoriginal = solofilenameoriginal;
		if (eslashDot > 0)
			filenameoriginal = filenameoriginal.substring(eslashDot + 1);
		int extDot = filenameoriginal.lastIndexOf('.');
		  String extension = filenameoriginal.substring(extDot + 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		DecimalFormat df = new DecimalFormat("000000");
		Timestamp diahoy = new Timestamp(System.currentTimeMillis());
		String filename = numeroSid + numeroAnio + df.format(idusuario)
				+ df.format(idarea) + df.format(idflujo) + i
				+ sdf.format(diahoy) + "2." + extension;
		
		return filename;
	}
	
	public static String getfechaLargaFormateada(Timestamp fec) {
		if (fec == null)
			return "";
		int S = 0, M = 0, A = 0, D = 0;
		Date fechax = new Date(fec.getTime());
		Calendar f = Calendar.getInstance();
		f.setTime(fechax);
		String di[] = { "", "domingo", "lunes", "martes", "miercoles",
				"jueves", "viernes", "sabado" };
		String me[] = { "enero", "febrero", "marzo", "abril", "mayo", "junio",
				"julio", "agosto", "septiembre", "octubre", "noviembre",
				"diciembre" };
		S = f.get(Calendar.DAY_OF_WEEK);
		M = f.get(Calendar.MONTH);
		D = f.get(Calendar.DAY_OF_MONTH);
		A = f.get(Calendar.YEAR);
		return di[S] + " " + D + " de " + me[M] + " de " + A; 
	}

	public static Timestamp getFechaStringToTimestamp(String fec) {
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy");
		Date fecha = null;
		try {
			fecha = formatoDelTexto.parse(fec);
		} catch (java.text.ParseException ex) {
			//ex.printStackTrace();
		}
		long lnMilisegundos = fecha.getTime();
		Timestamp sqlTimestamp = new java.sql.Timestamp(lnMilisegundos);
		return sqlTimestamp;
	}
	
	public static Timestamp getCurrentDateTimestamp(){
		Date date= new Date();
		Timestamp sqlTimestamp = new Timestamp(date.getTime());
		return sqlTimestamp;
	}
	
	
	public static Timestamp getDateByTimestamp(Date date){		
		Timestamp sqlTimestamp = new Timestamp(date.getTime());
		return sqlTimestamp;
	}
	
	
	
	
	public static boolean isNumeric(String cadena){
		try{
			
			if(cadena.length() <= 9 ){
				Integer.parseInt(cadena);
			}if(cadena.length() > 9){
				Float.parseFloat(cadena);
			}
			return true;
		}catch (NumberFormatException e) {
			return false;
		}
	}
	
	public static boolean isNumericDecimal(String cadena){
		try{			
				new BigDecimal(cadena); 
			return true;
		}catch (NumberFormatException e) {
			return false;
		}
	}	
	
	public static String getStringToDate(Date formatDate){
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy");
		String fecha = sdf.format(formatDate);
		return fecha;
	}

	public static String getDniCorrecto(String dni){
		boolean esDniNumero= isNumeric(dni);
		if(esDniNumero && dni.trim().length()<8){
			return String.format("%08d", new Integer(dni));
		}
		return dni;
	}
	
	public static String getCompletarCeros(int cantidad,String num){
		//boolean esNumero= isNumeric(num);		
		return String.format("%0"+cantidad+"d", new Integer(num));		
		
	}
	
	public static String getCampoToColumna(String columna){
		String[] cads= columna.toLowerCase().split("_");
		String cadCampo="";
		for(String cad:cads){
			
			if(cadCampo.equals("")){
				cadCampo=cadCampo.concat(cad);
			}else{
				cadCampo=cadCampo+Character.toUpperCase(cad.charAt(0))+cad.substring(1);
			}
		}
		
		return cadCampo;
	}


	public static String getResol(String resolucion){
		if(resolucion!=null && !resolucion.isEmpty() && resolucion.length()<=36){
			resolucion=resolucion.replaceAll("N�.","NUM.");
			resolucion=resolucion.replaceAll("N�", "NUM.");
			resolucion=resolucion.replaceAll("No.","NUM.");
			resolucion=resolucion.replaceAll("No", "NUM.");
			resolucion=resolucion.replaceAll("N�.","NUM.");
			resolucion=resolucion.replaceAll("N�", "NUM.");			
			resolucion=resolucion.replaceAll("N�.","NUM.");
			resolucion=resolucion.replaceAll("N�", "NUM.");
			
		}else if(resolucion!=null && !resolucion.isEmpty()){
			resolucion=resolucion.replaceAll("N�.","");
			resolucion=resolucion.replaceAll("N�", "");
			resolucion=resolucion.replaceAll("No.","");
			resolucion=resolucion.replaceAll("No", "");
			resolucion=resolucion.replaceAll("N�.","");
			resolucion=resolucion.replaceAll("N�", "");			
			resolucion=resolucion.replaceAll("N�.","");
			resolucion=resolucion.replaceAll("N�", "");
		}
		return resolucion;
	}
	public static String getNumExpJudicial(String expJudicial){
		if(expJudicial!=null && !expJudicial.isEmpty() && expJudicial.length()<=26){
			expJudicial=expJudicial.replaceAll("N�.","NUM.");
			expJudicial=expJudicial.replaceAll("N�", "NUM.");
			expJudicial=expJudicial.replaceAll("No.","NUM.");
			expJudicial=expJudicial.replaceAll("No", "NUM.");
			expJudicial=expJudicial.replaceAll("N�.","NUM.");
			expJudicial=expJudicial.replaceAll("N�", "NUM.");			
			expJudicial=expJudicial.replaceAll("N�.","NUM.");
			expJudicial=expJudicial.replaceAll("N�", "NUM.");
			
		}else if(expJudicial!=null && !expJudicial.isEmpty()){
			expJudicial=expJudicial.replaceAll("N�.","");
			expJudicial=expJudicial.replaceAll("N�", "");
			expJudicial=expJudicial.replaceAll("No.","");
			expJudicial=expJudicial.replaceAll("No", "");
			expJudicial=expJudicial.replaceAll("N�.","");
			expJudicial=expJudicial.replaceAll("N�", "");			
			expJudicial=expJudicial.replaceAll("N�.","");
			expJudicial=expJudicial.replaceAll("N�", "");
		}
		return expJudicial;
	}	
		
	public static Date getFechaStringToDate(String str_date){
		try {  
			DateFormat formatter ; 
			formatter = new SimpleDateFormat("yyyy-mm-dd");
			Date date; 
			date = (Date)formatter.parse(str_date);  
			return date;
		  } catch (ParseException e)
		  {
			  e.printStackTrace();
		  }  
		  return null;
	}
	

}
